Passed
Pull Request — master (#8)
by Johan
04:43 queued 02:16
created

( ➔ ... ➔ it(ꞌ"Get the summary for current position"ꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 1
rs 10
c 1
b 0
f 0
1
/*jslint node: true, nomen: true, regexp: true, plusplus: true */
2
(function () {
3
4
    'use strict';
5
6
    const chai = require("chai"),
7
        should = chai.should(),
8
        expect = chai.expect;
9
10
    const Blockfolio = require("../index");
11
12
    const FAKE_TOKEN = "1915f3d2ef313e86";
13
14
    // Enable regexps for chai
15
    chai.use(require('chai-match'));
16
17
    describe("Blockfolio API", function() {
18
        describe("General", function () {
19
            it("Get the API version", function (done) {
20
                Blockfolio.getVersion((err, version) => {
21
                    if (err) { return done(err); }
22
                    should.exist(version);
23
                    expect(version).to.be.a("number");
24
                    return done();
25
                });
26
            });
27
            it("Get the system status of the API", function (done) {
28
                Blockfolio.getStatus((err, statusMsg) => {
29
                    if (err) { return done(err); }
30
                    should.exist(statusMsg);
31
                    expect(statusMsg).to.be.a("string");
32
                    return done();
33
                });
34
            });
35
            it("Get the announcements from SIGNAL", function (done) {
36
                Blockfolio.getAnnouncements((err, announcements) => {
37
                    if (err) { return done(err); }
38
                    should.exist(announcements);
39
                    expect(announcements).to.be.an("array");
40
                    return done();
41
                });
42
            });
43
            it("should fail at registering an already activated DEVICE_TOKEN", function (done) {
44
                Blockfolio._register(FAKE_TOKEN, (err, response) => {
45
                    should.exist(err.message);
46
                    should.not.exist(response);
47
                    return done();
48
                });
49
            });
50
        });
51
        describe("Module Instanciation", function () {
52
            it("getPositions called without it should return an error", function (done) {
53
                Blockfolio.getPositions("BTC/USD", (err, positions) => {
54
                    should.exist(err.message);
55
                    expect(err.message).to.equal("A valid CLIENT_TOKEN should be provided! (Have you called Blockfolio.init()?)");
56
                    should.not.exist(positions);
57
                    return done();
58
                });
59
            });
60
            it("getMarketDetails called without it should return an error", function (done) {
61
                Blockfolio.getMarketDetails("BTC/USD").then(() => {
62
                    return done(new Error("Should not go here without a CLIENT_TOKEN!"));
63
                }).catch((err) => {
64
                    should.exist(err.message);
65
                    expect(err.message).to.equal("A valid CLIENT_TOKEN should be provided! (Have you called Blockfolio.init()?)");
66
                    return done();
67
                });
68
            });
69
            it("removeCoin called without it should return an error", function (done) {
70
                Blockfolio.removeCoin("BTC/USD").then(() => {
71
                    return done(new Error("Should not go here without a CLIENT_TOKEN!"));
72
                }).catch((err) => {
73
                    should.exist(err.message);
74
                    expect(err.message).to.equal("A valid CLIENT_TOKEN should be provided! (Have you called Blockfolio.init()?)");
75
                    return done();
76
                });
77
            });
78
            it("getHoldings called without it should return an error", function (done) {
79
                Blockfolio.getHoldings("BTC/USD").then(() => {
80
                    return done(new Error("Should not go here without a CLIENT_TOKEN!"));
81
                }).catch((err) => {
82
                    should.exist(err.message);
83
                    expect(err.message).to.equal("A valid CLIENT_TOKEN should be provided! (Have you called Blockfolio.init()?)");
84
                    return done();
85
                });
86
            });
87
            it("getPortfolioSummary called without it should return an error", function (done) {
88
                Blockfolio.getPortfolioSummary().then(() => {
89
                    return done(new Error("Should not go here without a CLIENT_TOKEN!"));
90
                }).catch((err) => {
91
                    should.exist(err.message);
92
                    expect(err.message).to.equal("A valid CLIENT_TOKEN should be provided! (Have you called Blockfolio.init()?)");
93
                    return done();
94
                });
95
            });
96
            it("should fail to initialize with a disposable token", function (done) {
97
                Blockfolio.init("40f027b891222cdf7fe7d7390a29e4bb5c79ea7adbab660c855b2d6c603de2d710c10aebcc4ee76c6da4402457cbfd50", (err) => {
98
                    should.exist(err.message);
99
                    return done();
100
                });
101
            });
102
            it("should initialize quickly with coin checks disabled", function (done) {
103
                Blockfolio.init(FAKE_TOKEN, { disableCoinCheck: true }).then(() => {
104
                    return done();
105
                }).catch((err) => { return done(err); });
106
            });
107
            it("addPosition should fail when disableCoinCheck is enabled", function (done) {
108
                Blockfolio.addPosition("BTC/USD").then(() => {
109
                    return done(new Error("Should not go here with disableCoinCheck: true!"));
110
                }).catch((err) => {
111
                    should.exist(err.message);
112
                    expect(err.message).to.equal("coinsList uninitialized, could not validate token pairs!");
113
                    return done();
114
                });
115
            });
116
            it("should fail to register with an existing token", function (done) {
117
                Blockfolio._register(FAKE_TOKEN).then(() => {
118
                    return done(new Error("Should not pass"));
119
                }).catch((err) => { should.exist(err.message); return done(); });
120
            });
121
            // Expand timeout for initialization
122
            this.timeout(30000);
123
            it("should be ok with a working token", function (done) {
124
                Blockfolio.init(FAKE_TOKEN).then(() => {
125
                    return done();
126
                }).catch((err) => {return done(err); });
127
            });
128
        });
129
        describe("Tools", function () {
130
            it("should return a random token", function (done) {
131
                const generatedToken = Blockfolio.utils.generateClientToken();
132
                expect(generatedToken).to.be.a("string");
133
                return done();
134
            });
135
            it("should convert properly XRP/BTC to a pair struct", function (done) {
136
                const pair = Blockfolio.utils.parseToken("XRP/BTC");
137
                expect(pair).to.be.deep.equal({ base: "BTC", token: "XRP" });
138
                return done();
139
            });
140
            it("should convert properly BTC/USD to a pair struct", function (done) {
141
                const pair = Blockfolio.utils.parseToken("BTC/USD");
142
                expect(pair).to.be.deep.equal({ base: "USD", token: "BTC" });
143
                return done();
144
            });
145
            it("should convert properly AEON to a pair struct", function (done) {
146
                const pair = Blockfolio.utils.parseToken("AEON");
147
                expect(pair).to.be.deep.equal({ base: "BTC", token: "AEON" });
148
                return done();
149
            });
150
            it("should convert properly BTC-LTC to a pair struct", function (done) {
151
                const pair = Blockfolio.utils.parseToken("BTC-LTC");
152
                expect(pair).to.be.deep.equal({ base: "BTC", token: "LTC" });
153
                return done();
154
            });
155
            it("should convert properly BTC-DASH to a pair struct", function (done) {
156
                const pair = Blockfolio.utils.parseToken("BTC-DASH");
157
                expect(pair).to.be.deep.equal({ base: "BTC", token: "DASH" });
158
                return done();
159
            });
160
            it("should convert properly BTC-BCH to a pair struct", function (done) {
161
                const pair = Blockfolio.utils.parseToken("BTC-BCH");
162
                expect(pair).to.be.deep.equal({ base: "BTC", token: "BCH" });
163
                return done();
164
            });
165
        });
166
        describe("Endpoints", function () {
167
            // Expand timeout for network & API lentency
168
            this.timeout(30000);
169
            describe("Misc", function () {
170
171
                it("Get the portfolio summary", function (done) {
172
                    Blockfolio.getPortfolioSummary().then((summary) => {
173
                        should.exist(summary);
174
                        expect(summary.btcValue).to.be.a("number");
175
                        return done();
176
                    }).catch((err) => {
177
                        return done(err);
178
                    });
179
                });
180
181
                it("Get the currencies list", function (done) {
182
                    Blockfolio.getCurrencies((err, currencies) => {
183
                        if (err) {
184
                            return done(err);
185
                        }
186
                        should.exist(currencies);
187
                        expect(currencies).to.be.an("array");
188
                        return done();
189
                    });
190
                });
191
192
                it("Get the coins list", function (done) {
193
                    Blockfolio.getCoinsList().then((coins) => {
194
                        should.exist(coins);
195
                        expect(coins).to.be.an("array");
196
                        return done();
197
                    }).catch((err) => { return done(err); });
198
                });
199
200
                it("Get a Disposable Device Token", function (done) {
201
                    Blockfolio.getDisposableDeviceToken().then((token) => {
202
                        should.exist(token);
203
                        expect(token).to.match(/[a-f0-9]{96}/);
204
                        return done();
205
                    }).catch((err) => { return done(err); });
206
                });
207
            });
208
            describe("Markets & Exchanges", function () {
209
210
                it("Get market details for an AEON/BTC on Bittrex", function (done) {
211
                    Blockfolio.getMarketDetails("AEON/BTC", {
212
                        exchange: "bittrex"
213
                    }, (err, details) => {
214
                        if (err) {
215
                            return done(err);
216
                        }
217
218
                        should.exist(details.ask);
219
                        expect(details.ask).to.be.a("string");
220
                        return done();
221
                    });
222
                });
223
224
                it("Get market details for an LTC/BTC on the top exchange", function (done) {
225
                    Blockfolio.getMarketDetails("LTC/BTC").then((details) => {
226
                        should.exist(details.ask);
227
                        expect(details.ask).to.be.a("string");
228
                        return done();
229
                    }).catch((err) => {
230
                        return done(err);
231
                    });
232
                });
233
234
                it("Get available exchanges for this token", function (done) {
235
                    Blockfolio.getExchanges("AEON/BTC", (err, exchanges) => {
236
                        if (err) {
237
                            return done(err);
238
                        }
239
240
                        expect(exchanges).to.be.an("array");
241
                        return done();
242
                    });
243
                });
244
245
                it("Get available exchanges for an incorrect token", function (done) {
246
                    Blockfolio.getExchanges("ZSKJD/BTC").then((exchanges) => {
247
                        should.not.exist(exchanges);
248
                    }).catch((err) => {
249
                        should.exist(err.message);
250
                        expect(err.message).to.equal("ZSKJD/BTC is not an available token on Blockfolio!");
251
                        return done();
252
                    });
253
                });
254
255
                it("Get the last price of an incorrect token on this exchange", function (done) {
256
                    Blockfolio.getPrice("EAZRREZREZ/BTC", {
257
                        exchange: "bittrex"
258
                    }, (err, rPrice) => {
259
                        should.exist(err.message);
260
                        expect(err.message).to.equal("EAZRREZREZ/BTC is not an available token on Blockfolio!");
261
                        should.not.exist(rPrice);
262
                        return done();
263
                    });
264
                });
265
266
                it("... and with a valid token, but an incorrect base", function (done) {
267
                    Blockfolio.getPrice("BTC/DSQFSDFDSF", {
268
                        exchange: "bittrex"
269
                    }).then(() => {
270
                        return done(new Error("Should not be here!"));
271
                    }).catch((err) => {
272
                        should.exist(err.message);
273
                        expect(err.message).to.equal("BTC/DSQFSDFDSF is not an available token on Blockfolio!");
274
                        return done();
275
                    });
276
                });
277
278
                it("Get the last price of ETH on the top exchange", function (done) {
279
                    Blockfolio.getPrice("ETH").then((cPrice) => {
280
                        expect(cPrice).to.be.a("number");
281
                        return done();
282
                    }).catch((err) => { return done(err); });
283
                });
284
285
                it("Get the last price of AEON on this exchange", function (done) {
286
                    Blockfolio.getPrice("AEON/BTC", {
287
                        exchange: "bittrex"
288
                    }, (err, cPrice) => {
289
                        if (err) { return done(err); }
290
291
                        expect(cPrice).to.be.a("number");
292
                        return done();
293
                    });
294
                });
295
            });
296
            describe("Positions", function () {
297
              
298
                it("Add a BTC position on this pair (buy)", function (done) {
299
                    Blockfolio.addPosition("AEON/BTC", {
300
                        exchange: "bittrex",
301
                        price: 0.00018,
302
                        amount: 200,
303
                        note: "AEON FTW"
304
                    }, (err) => {
305
                        if (err) { return done(err); }
306
307
                        return done();
308
                    });
309
                });
310
                it("Add another BTC position on this pair (sell)", function (done) {
311
                    Blockfolio.addPosition("AEON/BTC", {
312
                        mode: "sell",
313
                        exchange: "bittrex",
314
                        price: 0.00018,
315
                        amount: 10,
316
                        note: "AEON FTW"
317
                    }, (err) => {
318
                        if (err) { return done(err); }
319
320
                        return done();
321
                    });
322
                });
323
324
                it("Get the summary for current position", function (done) {
325
                    Blockfolio.getHoldings("AEON/BTC").then((summary) => {
326
                        should.exist(summary.holdingValueString);
327
                        expect(summary.holdingValueString).to.be.a("string");
328
                        return done();
329
                    }).catch((err) => { return done(err); });
330
                });
331
332
                var posId = 0;
333
                it("Get orders details for this position", function (done) {
334
                    Blockfolio.getPositions("AEON/BTC").then((positions) => {
335
                        should.exist(positions);
336
                        expect(positions).to.be.an("array");
337
                        posId = positions[0].positionId;
338
                        return done();
339
                    }).catch((err) => { return done(err); });
340
                });
341
342
                it("Remove the just added position", function (done) {
343
                    Blockfolio.removePosition(posId).then(() => {
344
                        return done();
345
                    }).catch((err) => { return done(err); });
346
                });
347
348
                it("And then remove completely the coin from portfolio", function (done) {
349
                    Blockfolio.removeCoin("AEON/BTC", (err, res) => {
350
                        if (err) { return done(err); }
351
352
                        expect(res).to.equal("success");
353
                        return done();
354
                    });
355
                });
356
357
                it("Get actual positions left", function (done) {
358
                    Blockfolio.getPositions((err, positions) => {
359
                        if (err) { return done(err); }
360
361
                        should.exist(positions);
362
                        expect(positions).to.be.an("array");
363
                        return done();
364
                    });
365
                });
366
367
                it("Add a token pair to watch from Bittrex", function (done) {
368
                    Blockfolio.addPosition("AEON/BTC", {exchange: "bittrex"}, (err) => {
369
                        if (err) return done(err);
370
                        return done();
371
                    });
372
                });
373
374
                it("Watch LTC/BTC from the top exchange", function (done) {
375
                    Blockfolio.addPosition("LTC/BTC").then(() => {
376
                        return done();
377
                    }).catch((err) => {
378
                        return done(err);
379
                    });
380
                });
381
382
                it("addPosition should fail when no parameter is passed", function (done) {
383
                    Blockfolio.addPosition().then(() => {
384
                        return done(new Error("Should not go here with no pair!"));
385
                    }).catch((err) => {
386
                        should.exist(err.message);
387
                        expect(err.message).to.equal("You must provide a token to add to your position!");
388
                        return done();
389
                    });
390
                });
391
                it("removePosition should fail when no parameter is passed", function (done) {
392
                    Blockfolio.removePosition().then(() => {
393
                        return done(new Error("Should not go here with no positionId!"));
394
                    }).catch((err) => {
395
                        should.exist(err.message);
396
                        expect(err.message).to.equal("You must provide a position ID!");
397
                        return done();
398
                    });
399
                });
400
401
            });
402
            describe("Alerts", function () {
403
404
                it("Add an alert when LTC/EUR crosses 200", function (done) {
405
                    Blockfolio.addAlert("LTC/EUR", {
406
                        above: 200
407
                    }).then(() => {
408
                        return done();
409
                    }).catch((err) => {
410
                        return done(err);
411
                    });
412
                });
413
414
                it("Add an alert when BTC/EUR crosses 20000 on Coinbase", function (done) {
415
                    Blockfolio.addAlert("BTC/EUR", {
416
                        above: 20000,
417
                        exchange: "Coinbase"
418
                    }).then(() => {
419
                        return done();
420
                    }).catch((err) => {
421
                        return done(err);
422
                    });
423
                });
424
425
                it("Add an alert when ETH/EUR crosses 2000 on Bittrex with persistence", function (done) {
426
                    Blockfolio.addAlert("ETH/EUR", {
427
                        above: 2000,
428
                        exchange: "bittrex",
429
                        persistent: true
430
                    }).then(() => {
431
                        return done();
432
                    }).catch((err) => {
433
                        return done(err);
434
                    });
435
                });
436
437
                let alertToDelete = 1;
438
                it("Get the alerts for LTC/EUR", function (done) {
439
                    Blockfolio.getAlerts("LTC/EUR").then((alerts) => {
440
                        expect(alerts).to.be.an("array");
441
                        alertToDelete = alerts[0].alertId;
442
                        return done();
443
                    }).catch((err) => {
444
                        return done(err);
445
                    });
446
                });
447
448
                it("Pause all alerts on LTC/EUR", function (done) {
449
                    Blockfolio.pauseAllAlerts("LTC/EUR").then(() => {
450
                        return done();
451
                    }).catch((err) => {
452
                        return done(err);
453
                    });
454
                });
455
456
                it("... And restart them", function (done) {
457
                    Blockfolio.startAllAlerts("LTC/EUR").then(() => {
458
                        return done();
459
                    }).catch((err) => {
460
                        return done(err);
461
                    });
462
                });
463
464
                it("Pause just the last added alert", function (done) {
465
                    Blockfolio.pauseAlert(alertToDelete).then(() => {
466
                        return done();
467
                    }).catch((err) => {
468
                        return done(err);
469
                    });
470
                });
471
472
                it("... Restart it", function (done) {
473
                    Blockfolio.startAlert(alertToDelete).then(() => {
474
                        return done();
475
                    }).catch((err) => {
476
                        return done(err);
477
                    });
478
                });
479
480
                it("... And then remove it!", function (done) {
481
                    Blockfolio.removeAlert(alertToDelete).then(() => {
482
                        return done();
483
                    }).catch((err) => {
484
                        return done(err);
485
                    });
486
                });
487
                it("addAlert should fail when no parameter is passed", function (done) {
488
                    Blockfolio.addAlert().then(() => {
489
                        return done(new Error("Should not go here with no pair!"));
490
                    }).catch((err) => {
491
                        should.exist(err.message);
492
                        expect(err.message).to.equal("You must provide a pair to set alerts!");
493
                        return done();
494
                    });
495
                });
496
                it("addAlert should fail when boundaries are missing", function (done) {
497
                    Blockfolio.addAlert("XMR/BTC").then(() => {
498
                        return done(new Error("Should not go here with no boundaries!"));
499
                    }).catch((err) => {
500
                        should.exist(err.message);
501
                        expect(err.message).to.equal("You must specify at leat a boundary to set up an alert!");
502
                        return done();
503
                    });
504
                });
505
                it("getAlerts should fail when no parameter is passed", function (done) {
506
                    Blockfolio.getAlerts().then(() => {
507
                        return done(new Error("Should not go here with no pair!"));
508
                    }).catch((err) => {
509
                        should.exist(err.message);
510
                        expect(err.message).to.equal("You must provide a pair to get alerts from!");
511
                        return done();
512
                    });
513
                });
514
                it("pauseAlert should fail when no parameter is passed", function (done) {
515
                    Blockfolio.pauseAlert().then(() => {
516
                        return done(new Error("Should not go here with no alertId!"));
517
                    }).catch((err) => {
518
                        should.exist(err.message);
519
                        expect(err.message).to.equal("You must provide an alert ID!");
520
                        return done();
521
                    });
522
                });
523
                it("startAlert should fail when no parameter is passed", function (done) {
524
                    Blockfolio.startAlert().then(() => {
525
                        return done(new Error("Should not go here with no alertId!"));
526
                    }).catch((err) => {
527
                        should.exist(err.message);
528
                        expect(err.message).to.equal("You must provide an alert ID!");
529
                        return done();
530
                    });
531
                });
532
                it("pauseAllAlerts should fail when no parameter is passed", function (done) {
533
                    Blockfolio.pauseAllAlerts().then(() => {
534
                        return done(new Error("Should not go here with no pair!"));
535
                    }).catch((err) => {
536
                        should.exist(err.message);
537
                        expect(err.message).to.equal("You must provide a pair to pause alerts!");
538
                        return done();
539
                    });
540
                });
541
                it("startAllAlerts should fail when no parameter is passed", function (done) {
542
                    Blockfolio.startAllAlerts().then(() => {
543
                        return done(new Error("Should not go here with no pair!"));
544
                    }).catch((err) => {
545
                        should.exist(err.message);
546
                        expect(err.message).to.equal("You must provide a pair to start alerts!");
547
                        return done();
548
                    });
549
                });
550
                it("removeAlert should fail when no parameter is passed", function (done) {
551
                    Blockfolio.removeAlert().then(() => {
552
                        return done(new Error("Should not go here with no alertId!"));
553
                    }).catch((err) => {
554
                        should.exist(err.message);
555
                        expect(err.message).to.equal("You must provide an alert ID!");
556
                        return done();
557
                    });
558
                });
559
            });
560
        });
561
    });
562
563
})();